package test;

import io.rsocket.Payload;
import io.rsocket.RSocket;
import io.rsocket.SocketAcceptor;
import io.rsocket.core.RSocketServer;
import io.rsocket.exceptions.RejectedSetupException;
import io.rsocket.exceptions.SetupException;
import io.rsocket.transport.netty.server.CloseableChannel;
import io.rsocket.transport.netty.server.TcpServerTransport;
import io.rsocket.util.DefaultPayload;
import reactor.core.publisher.Mono;

import java.util.logging.Logger;

public class Server001 {

    static Logger LOG = Logger.getAnonymousLogger();

    public static void main(String[] args) throws Exception {

        SocketAcceptor socketAcceptor = (setup, sendingSocket) -> {
            LOG.info("Incoming connection ....");
            final RSocket responder = new RSocket() {
                @Override
                public Mono<Payload> requestResponse(Payload payload) {
                    String t = payload.getDataUtf8();
                    return Mono.just(DefaultPayload.create(t + " World !"));
                }

                @Override
                public void dispose() {
                    LOG.info("Disposing ");
                }
            };
            String d = setup.getDataUtf8();
            if(d.equals("ab")){
                throw new RejectedSetupException("The data is ab");
            }
            return Mono.just(responder);
        };

        CloseableChannel closeableChannel = RSocketServer.create()
                .acceptor(socketAcceptor)
                .bind(TcpServerTransport.create(80))
                .doOnNext(s -> {
                    LOG.info("Listen on :" + s.address().toString());
                })
                .block();
        Sleeps.waitForExit();
    }
}
